package com.zzq.my.jetpacks.jectpackapp

import android.util.Log
import android.util.Log.d
import android.view.LayoutInflater
import android.view.View
import by.kirich1409.viewbindingdelegate.viewBinding
import com.aisier.architecture.base.BaseActivity
import com.orhanobut.logger.Logger
import com.zzq.my.jetpacks.jectpackapp.adapter.RvAdapter
import com.zzq.my.jetpacks.jectpackapp.bean.ItemInfo
import com.zzq.my.jetpacks.jectpackapp.bean.createTradeFilterOptionConfig
import com.zzq.my.jetpacks.jectpackapp.databinding.ActivityMainBinding
import com.zzq.my.jetpacks.jectpackapp.decoration.MyDecoration
import com.zzq.my.jetpacks.jectpackapp.decoration.SpacesItemDecoration
import com.zzq.my.jetpacks.jectpackapp.ui.act.*
import com.zzq.my.jetpacks.jectpackapp.ui.dzfs.ArrivalMethodAct
import com.zzq.my.jetpacks.jectpackapp.ui.dzfs.WithdrawAct
import com.zzq.my.jetpacks.jectpackapp.ui.scan.PlayLoadingAct
import com.zzq.my.jetpacks.jectpackapp.ui.scan.PlayStatusAct
import com.zzq.my.jetpacks.jectpackapp.ui.scan.PlayTickAct
import com.zzq.my.jetpacks.util.goActivity
import com.zzq.my.jetpacks.util.launchKt
import kotlinx.coroutines.suspendCancellableCoroutine
import java.util.logging.Logger
import kotlin.math.log


/*
  * ================================================
  * 描述：封装Retrofit+协程，实现优雅快速的网络请求
  * 作者：zhuangzeqin
  * 时间: 2021/11/25-10:52
  * 邮箱：zzq@eeepay.cn
  * 备注:
  * ----------------------------------------------------------------
  * You never know what you can do until you try !
  *      _              _           _     _   ____  _             _ _
  *     / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
  *    / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
  *   / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
  *  /_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
  *
  * 签名：最痛苦的事不是我失败了，而是我本可以.--zzq
  * ----------------------------------------------------------------
  * ================================================
  */

class MainActivity : BaseActivity(R.layout.activity_main) {
    //    private val mLoginViewModel by viewModels<LoginViewModel>()
//    private val mLoadIndexDataVM by viewModels<LoadIndexDataVM>()
//    private val adapter = MultiTypeAdapter()
//    private val items = ArrayList<Any>()
    private val mMainBindingView by viewBinding(ActivityMainBinding::bind)

    private lateinit var rvAdapter: RvAdapter

    /**
     * 初始化数据
     */
    override fun initData() {
//        gotoActivityForResult<HttpAct> {
//            val text = it?.getStringExtra("text")
//            toast("拿到返回数据：$text")
//        }

        run loop@{
            (1..7).forEach {
                if (it == 3) {
                    return@loop//退出整个循环
                }
                Log.d("xys", "Num: $it")
            }
        }

        val bean = createTradeFilterOptionConfig{

        }
        Logger.d("我是自己创建的bean：$bean")

        (1..7).asSequence().takeWhile { it != 3 }.forEach { println(it)  }

//        data.also {
//            it.forEachIndexed { index, info ->
//                if (info.sysValue==payTypeParent)
//                {
//                    item = info
//                    return@also
//                }
//            }
//        }
    }

    /**
     * 初始化操作
     */
    override fun initView() {
        rvAdapter = RvAdapter()
        val mutableListOf = mutableListOf<ItemInfo>()
        val listOf = listOf(
            "网络请求封装",
            "ConcatAdapter合并多个rv适配器",
            "androidx Fragment 懒加载",
            "FlexboxLayout弹性布局",
            "Rxhttp 网络请求封装",
            "ShapeView阴影基本用法",
            "MMKV 的基本使用",
            "Flow 的基本是使用",
            "fresco 强大的图片加载框架",
            "APP 引导页",
            "fresco 强大的图片加载框架",
            "ActivityMessenger简化Activity之间传参和回调的逻辑代码。",
            "step指示器动态添加",
            "到账方式",
            "账号提现",
            " 实现带空格分隔的输入电话号码",
            " NestedScrollView滚动到顶部固定子View悬停挂靠粘在顶端",
            " ViewPager listView 滑动冲突解决",
            " Scrollview rv 滑动冲突解决",
            " HeaderScrollViewActivity 吸顶",
            " PlayLoadingAct 支付页面",
            " PlayStatusAct 支付状态页面",
            " PlayTickAct 签购小票",
            " BottomSheetActivity",
            " ShapeableImageViewActivity",
            " ViewPager2 轮播图",
            " GrowUpCenterAct 成长中心",
            " ReclyeView 网格线分隔",
            " CoordinatorLayout 管理子View或者子View之间的联动交互",
        )
        listOf.forEach {
            mutableListOf.add(ItemInfo(it))
        }
        rvAdapter.setList(mutableListOf)
        val noDataView =
            LayoutInflater.from(mContext)
                .inflate(R.layout.lib_res_layout_nodata_foot, null) //没有数据的
//        rvAdapter.addFooterView(view = noDataView,orientation =  LinearLayout.HORIZONTAL)
        //添加自定义分割线：可自定义分割线高度和颜色
//        mMainBindingView.rvList.addItemDecoration(
////            DividerItemUtils.dividerItemDecorationTool(mContext)//默认的间隔线
//            DividerItemUtils.dividerItemDecorationTool(
//                mContext,
//                drawId = R.drawable.divider_mileage
//            )//自定义间隔线
//        )
        // todo  设置分割线color 1
        val itemDecoration =  SpacesItemDecoration(mContext, SpacesItemDecoration.HORIZONTAL, 0, 1)
            .setParam(R.color.color_3887EB, 1, 10F, 10F)
        //todo 设置分割线2
        val itemDecoration2 =  SpacesItemDecoration(mContext, SpacesItemDecoration.VERTICAL, 0, 1)
            .setDrawable(R.drawable.divider_mileage)
        //todo 设置分割线3
        val  itemDecoration3 =  MyDecoration(mContext)
        itemDecoration3.setColor(R.color.color_3887EB)


        mMainBindingView.rvList.addItemDecoration(itemDecoration3)
        mMainBindingView.rvList.adapter = rvAdapter//适配器
        //首页弹窗顺序执行
        launchKt {
            runCatching {
                showDialog01("我是系统公告弹窗1")
                showDialog02("我是系统公告弹窗2")
                showDialog03("我是系统公告弹窗3")
            }
        }
    }



    private suspend fun showDialog01(msg: String) = suspendCancellableCoroutine<String> { continuation ->
//        launchKt {
//            val resultDeferred = userViewModel.userRepo.userMainInfo()
//            Log.d("userViewModel", "initData: resultDeferred1")
//            if (!TextUtils.isEmpty(resultDeferred?.agentNo)) {
//                CommomDialog.with(context).setMessage(msg)
//                    .setOnCommomDialogListener(object : CommomDialog.OnCommomDialogListener {
//                        override fun onPositiveClick(v: View?) {
//                        }
//
//                        override fun onNegativeClick(v: View?) {
//
//                        }
//                    }).setOnCommomDismissListener {
//                        //todo 用到协程挂起的特性, 然后在 OnDismiss()回调中将协程恢复, 为了将这种基于回调的办法包装成协程挂起函数, 可以使用suspendCancellableCoroutine函数
//                        //todo 将一个回调函数转换成具有返回值的挂起函数
//                        continuation.resume("ok")
//                    }.show()
//            }
//        }
    }

    /**
     * 初始化点击事件操作
     */
    override fun eventOnClick() {
        //::rvAdapter.isInitialized用于判断adapter是否已经初始化
        if (::rvAdapter.isInitialized) {
            rvAdapter.setOnItemClickListener { adapter, view, position ->
                val item = adapter.getItem(position) as ItemInfo
//                Logger.t("item").d(item.name)
                when (position) {
                    0 -> goActivity<HttpAct>(mContext)//todo  网络请求封装
                    1 -> goActivity<ConcatAdapterAct>(mContext)//todo 按顺序添加的适配器RV 的 adapter
                    2 -> goActivity<FragLazyAct>(mContext) //todo androidx Fragment 懒加载
                    3 -> goActivity<FlexboxLayoutAct>(mContext)
                    4 -> goActivity<RxHttpAct>(mContext)
                    5 -> goActivity<ShapeViewActivity>(mContext)
                    6 -> goActivity<MMKVActivity>(mContext)
                    7 -> goActivity<FlowActivity>(mContext)
                    8 -> goActivity<FrescoActivity>(mContext)
                    12 -> goActivity<Activity8>(mContext)
                    13 -> goActivity<ArrivalMethodAct>(mContext)
                    14 -> goActivity<WithdrawAct>(mContext)
                    15 -> goActivity<EdtiextAct>(mContext)
                    16 -> goActivity<Activity10>(mContext)
                    17 -> goActivity<Activity11>(mContext)
                    18 -> goActivity<Activity12>(mContext)
                    19 -> goActivity<HeaderScrollViewActivity>(mContext)
                    20 -> goActivity<PlayLoadingAct>(mContext)
                    21 -> goActivity<PlayStatusAct>(mContext)
                    22 -> goActivity<PlayTickAct>(mContext)
                    23 -> goActivity<BottomSheetActivity>(mContext)
                    24 -> goActivity<ShapeableImageViewActivity>(mContext)
                    25 -> goActivity<ViewPager2BannerAct>(mContext)
                    25 -> goActivity<GrowUpCenterAct>(mContext)
                    26 -> goActivity<RecyleViewMenuAct>(mContext)
                    28 -> goActivity<CoordinatorLayoutAct>(mContext)
                }

            }
        }
    }
}